<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeDialog"
    @open="openDialog"
  >
    <!-- 权限点数据展示 -->
    <el-tree
      ref="tree"
      :data="permissionList"
      :props="{ label: 'name' }"
      :default-expand-all="true"
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <template #footer>
      <div style="text-align: right;">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { getRoleDetailById } from '@/api/setting'
import { assignPerm } from '@/api/setting'
import transTree from '@/utils/transTree'
export default {
  props: {
    showAssignDialog: {
      type: Boolean
    },
    curId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      permissionList: []
    }
  },
  methods: {
    closeDialog() {
      this.$emit('close-dialog')
    },
    async openDialog() {
      const res = await getPermissionList()
      // 树形数据处理
      this.permissionList = transTree(res.data)
      // 获取当前角色已经分配过的权限详情数据
      const resDetail = await getRoleDetailById(this.curId)
      // 调用组件方法完成回显
      this.$refs.tree.setCheckedKeys(resDetail.data.permIds)
      console.log(resDetail)
    },
    async confirm() {
      await assignPerm({
        id: this.curId,
        permIds: this.$refs.tree.getCheckedKeys()

      })
      this.$message.success('分配成功')
      this.closeDialog()
    }
  }
}
</script>
